/*
********************************************************************************
*
*	file : mian.c
*
********************************************************************************
*/
#include "sys_task.h" 
#include "pincfg.h"
#include "bsp_hal.h"
#include "sys_com.h"
#include "dataproc.h"
#include "mytype.h"
#include "swap.h"
#include "mbcom.h"
#include "usb_file.h" 
#include "flash_task.h" 
#include "simp_txt_analysis.h" 
#include "flash_cfg.h"

/*
********************************************************************************
*                               fun
********************************************************************************
*/
extern const txt_cmd_cfg_type cmd_list[];

const char file_info_name[] = "0:YS/INFO.TXT";

const char * fo_list[] = 
{
    "8N2","8O1","8E1","8N1",
};

/*******************************************************************************
* @brief  usb_file_info_handle.
* \param[in] none
* \retval: none
*******************************************************************************/
void usb_file_info_handle(void)
{
    int index = 0, cnt;
    
    if (f_open(&usb_opt.file, file_info_name, FA_WRITE | FA_CREATE_ALWAYS) != FR_OK)
    {
        return ;
    } 
    
    index += sprintf((char *)&usb_opt.file_buff[index],"-----------------------------------------------------------\r\n");
    index += sprintf((char *)&usb_opt.file_buff[index],"---------------------- SYSTEM CONFIG ----------------------\r\n");
    index += sprintf((char *)&usb_opt.file_buff[index],"-----------------------------------------------------------\r\n");
    
    index += sprintf((char *)&usb_opt.file_buff[index],"dev.name  : %s\r\n", sys_st.sys_cfg.dev_type);
    index += sprintf((char *)&usb_opt.file_buff[index],"dev.id    : %s\r\n", sys_st.sys_cfg.dev_id);
    index += sprintf((char *)&usb_opt.file_buff[index],"usr.name  : %s\r\n", sys_st.sys_cfg.usr_type);
    index += sprintf((char *)&usb_opt.file_buff[index],"usr.id    : %s\r\n", sys_st.sys_cfg.usr_id);
    index += sprintf((char *)&usb_opt.file_buff[index],"out       : %s\r\n", sys_st.sys_cfg.out_encode?"MDT":"TXT");
    
    index += sprintf((char *)&usb_opt.file_buff[index],"com1.id   : %d\r\n", sys_st.sys_cfg.com1.mb_id);
    index += sprintf((char *)&usb_opt.file_buff[index],"com1.baud : %d\r\n", sys_st.sys_cfg.com1.buad);
    index += sprintf((char *)&usb_opt.file_buff[index],"com1.fo   : %s\r\n", fo_list[sys_st.sys_cfg.com1.fo]);
    index += sprintf((char *)&usb_opt.file_buff[index],"com2.id   : %d\r\n", sys_st.sys_cfg.com2.mb_id);
    index += sprintf((char *)&usb_opt.file_buff[index],"com2.baud : %d\r\n", sys_st.sys_cfg.com2.buad);
    index += sprintf((char *)&usb_opt.file_buff[index],"com2.fo   : %s\r\n", fo_list[sys_st.sys_cfg.com2.fo]);
    
    index += sprintf((char *)&usb_opt.file_buff[index],"config cnt: %d\r\n", sys_st.sys_cfg.save_cnt);
    
	// 保存
	f_write(&usb_opt.file, &usb_opt.file_buff[0], index, &usb_opt.file_read_len_back); 
	index = 0;
	
    if(hist_dat_cfg.info->site_next >= hist_dat_cfg.info->site_start)
        index += sprintf((char *)&usb_opt.file_buff[index],"history   : %d\r\n", hist_dat_cfg.info->site_next - hist_dat_cfg.info->site_start - 1);
    else
        index += sprintf((char *)&usb_opt.file_buff[index],"history   : %d\r\n", hist_dat_cfg.info->blocks + hist_dat_cfg.info->site_next - hist_dat_cfg.info->site_start - 1);
    
    index += sprintf((char *)&usb_opt.file_buff[index],"\r\n\r\n");
    index += sprintf((char *)&usb_opt.file_buff[index],"-----------------------------------------------------------\r\n");
    index += sprintf((char *)&usb_opt.file_buff[index],"------------------------CMD SURPORT------------------------\r\n");
    index += sprintf((char *)&usb_opt.file_buff[index],"-----------------------------------------------------------\r\n");
    
	// 保存
	f_write(&usb_opt.file, &usb_opt.file_buff[0], index, &usb_opt.file_read_len_back); 
	index = 0;
	
    for(cnt = 0;cmd_list[cnt].format != TXT_CMD_TYPE_END; cnt++)
    {
        int amount;
        
        //跳过工厂设置命令
        if(cmd_list[cnt].attr & 0x8000)
            continue;
        if(cmd_list[cnt].attr & 0x4000)
            continue;
        
        index += sprintf((char *)&usb_opt.file_buff[index],"%-7s  <>   ", cmd_list[cnt].cmd);
        
        switch(cmd_list[cnt].format)
        {
            case TXT_CMD_TYPE_CHAR:
            {
                index += sprintf((char *)&usb_opt.file_buff[index],"sample :: %s = xxx", cmd_list[cnt].cmd);
                for(amount=1; amount < cmd_list[cnt].len_max; amount++)
                    index += sprintf((char *)&usb_opt.file_buff[index],", xxx");
                    index += sprintf((char *)&usb_opt.file_buff[index],"\r\n");
                break;
            }
            case TXT_CMD_TYPE_SHORT:
            {
                index += sprintf((char *)&usb_opt.file_buff[index],"sample :: %s = xxx", cmd_list[cnt].cmd);
                for(amount=1; amount < cmd_list[cnt].len_max; amount++)
                    index += sprintf((char *)&usb_opt.file_buff[index],", xxx");
                    index += sprintf((char *)&usb_opt.file_buff[index],"\r\n");
                break;
            }
            case TXT_CMD_TYPE_LONG:
            {
                index += sprintf((char *)&usb_opt.file_buff[index],"sample :: %s = xxx", cmd_list[cnt].cmd);
                for(amount=1; amount < cmd_list[cnt].len_max; amount++)
                    index += sprintf((char *)&usb_opt.file_buff[index],", xxx");
                    index += sprintf((char *)&usb_opt.file_buff[index],"\r\n");
                break;
            }
            case TXT_CMD_TYPE_FLOAT:
            {
                index += sprintf((char *)&usb_opt.file_buff[index],"sample :: %s = xxx.x", cmd_list[cnt].cmd);
                for(amount=1; amount < cmd_list[cnt].len_max; amount++)
                    index += sprintf((char *)&usb_opt.file_buff[index],", xxx.x");
                    index += sprintf((char *)&usb_opt.file_buff[index],"\r\n");
                break;
            }
            case TXT_CMD_TYPE_STRING:
            {
                index += sprintf((char *)&usb_opt.file_buff[index],"sample :: %s = string \r\n", cmd_list[cnt].cmd);
                break;
            }
            case TXT_CMD_TYPE_NUMBER:
            {
                index += sprintf((char *)&usb_opt.file_buff[index],"sample :: xxx \r\n");
                break;
            }
            case TXT_CMD_TYPE_NOP:
            {
                index += sprintf((char *)&usb_opt.file_buff[index],"sample :: %s\r\n", cmd_list[cnt].cmd);
                break;
            }
            default:
            {
                break;
            }
        }
		
		// 保存
		if(index >= FILE_BUFF_SIZE/2)
		{
			f_write(&usb_opt.file, &usb_opt.file_buff[0], index, &usb_opt.file_read_len_back); 
			index = 0;
		}
    }
    
    index += sprintf((char *)&usb_opt.file_buff[index],"\r\n");
    index += sprintf((char *)&usb_opt.file_buff[index],"xxx is a number !\r\n");
    index += sprintf((char *)&usb_opt.file_buff[index],"-----------------------------------------------------------\r\n");
    index += sprintf((char *)&usb_opt.file_buff[index],"------------------------    END    ------------------------\r\n");
    index += sprintf((char *)&usb_opt.file_buff[index],"-----------------------------------------------------------\r\n");
    
    f_write(&usb_opt.file, &usb_opt.file_buff[0], index, &usb_opt.file_read_len_back); 
    
    
    f_close(&usb_opt.file);
}

